package ro.gateway.aida.locations;

import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDALocation;
import ro.gateway.aida.obj.db.AIDALocationDB;
import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.srv.ICFService;
import ro.gateway.aida.utils.HttpUtils;


/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FinderAction.java,v 1.1 2004/10/24 23:37:41 mihaipostelnicu Exp $
 */
public class FinderAction extends Action {

    public ActionForward execute ( ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
        throws Exception {
        ServletContext application = this.getServlet().getServletContext();
        PersistenceToken token = PersistenceManager.tokenLookup( application );
        Hashtable services = ( Hashtable ) application.
            getAttribute( Constants.SERVICES );
        ICFService cfService = ( ICFService ) services.get( Constants.SERVICE_CF );

        LocationForm location_form = ( LocationForm ) form;
        location_form.clear_objects();
        location_form.action = HttpUtils.
            getValidTrimedString( request, "action", null );

        if ( "new".equals( location_form.action ) ) {
            location_form.reset_data();
            return mapping.findForward( "finder_jsp" );
        }

        if ( "save".equals( location_form.action ) ) {
//                  System.out.println( "Saving @index:" + location_form.selected_index );
            AIDALocation[] locations = location_form.getLocations();
            if ( ( locations != null )
                && ( location_form.selected_index < locations.length ) &&
                ( location_form.selected_index >= 0 ) ) {
                AIDALocation selected = locations[ location_form.selected_index ];

                location_form.cregion_id = selected.getCregion() != null ? selected.getCregion().getId() : -1;
                location_form.country_iso3 = selected.getCountry().getIso3();
                location_form.region_id = selected.getReg_id();
                location_form.county_id = selected.getJud_id();
                location_form.town_id = selected.getOras() != null ?
                    selected.getOras().getSirsup_id() : -1;
                location_form.village_id = selected.getLocalitate() != null ?
                    selected.getLocalitate().getSiruta_id() : -1;

                return mapping.findForward( "finnish" );
            }
        }

        location_form.what = HttpUtils.
            getValidTrimedString( request, "what", null );
        if ( location_form.what != null ) {
            location_form.action = "Search";
        }

        if ( "Search".equals( location_form.action )
            && ( location_form.what != null ) ) {
            location_form.setLocations( AIDALocationDB.getManager( token ).findLocations( location_form.what, 10 ) );
            location_form.filterFinderResults();
            if ( location_form.getLocations() == null || location_form.getLocations().length <= 0 ) {
                request.setAttribute( "search_failed", "true" );
//                        System.out.println( "search_failed" );
            } else {
                AIDALocation[] locations = location_form.getLocations();
                for ( int i = 0; i < locations.length; i++ ) {
                    AIDALocation location = locations[ i ];
                    if ( location.getCountry() != null ) {
                        location.setCregion_id( location.getCountry().getCregion_id() );
                        location.setCregion( null );
                    }
                    location.setCregion( AIDALocationDB
                        .getManager( token ).getCRegionById( location.getCregion_id() ) );
//                    System.out.println( "found:" + location.getCregion() );
                }
                request.setAttribute( "result_set",
                    location_form.getLocations() );
            }
        } else if ( "cancel".equals( location_form.action ) ) {
            return mapping.findForward( "finnish" );
        } else {
            location_form.setLocations( null );
            location_form.setWhat( null );
        }

        return mapping.findForward( "finder_jsp" );
    }
}
